using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for AdDao
/// </summary>
public class AdDao
{
    public AdDao() { }

    public DataTable LoadAds()
    {
        string proc = "sp_LoadAds";
        DataSet ds = DbProvider.LoadData(proc);
        return ds.Tables[0];
    }

    public DataTable LoadAllAds()
    {
        string proc = "sp_LoadAllAds";
        DataSet ds = DbProvider.LoadData(proc);
        return ds.Tables[0];
    }

    public Ad GetDto(decimal adID)
    {
        string proc = "sp_LoadAd";
        SqlParameter p = new SqlParameter("@AdID", SqlDbType.Decimal);
        p.Value = adID;

        DataSet ds = DbProvider.LoadData(proc, p);
        if (ds.Tables[0].Rows.Count == 0) return null;

        DataRow row = ds.Tables[0].Rows[0];

        string adText = row["AdText"].ToString();
        string adUrl = row["AdUrl"].ToString();
        string adImg = row["AdImg"].ToString();
        bool disabled = Convert.ToBoolean(row["Disabled"]);
        Ad dto = new Ad(adID, adText, adUrl, adImg, disabled);

        ds.Dispose();
        return dto;
    }

    public void Delete(decimal adID)
    {
        SqlCommand cmd = DbProvider.CreateCommand("sp_DeleteAd");
        cmd.Parameters.Add("@AdID", SqlDbType.Decimal);
        cmd.Parameters["@AdID"].Value = adID;
        DbProvider.ExecCmd(cmd);
    }

    public void Update(Ad dto)
    {
        SqlCommand cmd = DbProvider.CreateCommand("sp_UpdateAd");
        cmd.Parameters.Add("@AdID", SqlDbType.Decimal);
        cmd.Parameters.Add("@AdText", SqlDbType.NVarChar);
        cmd.Parameters.Add("@AdUrl", SqlDbType.NVarChar);
        cmd.Parameters.Add("@AdImg", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Disabled", SqlDbType.Bit);

        cmd.Parameters["@AdID"].Value = dto.AdID;
        cmd.Parameters["@AdText"].Value = dto.AdText;
        cmd.Parameters["@AdUrl"].Value = dto.AdUrl;
        cmd.Parameters["@AdImg"].Value = dto.AdImg;
        cmd.Parameters["@Disabled"].Value = dto.Disabled;

        DbProvider.ExecCmd(cmd);
    }

    public void Add(Ad dto)
    {
        SqlCommand cmd = DbProvider.CreateCommand("sp_AddAd");
        cmd.Parameters.Add("@AdText", SqlDbType.NVarChar);
        cmd.Parameters.Add("@AdUrl", SqlDbType.NVarChar);
        cmd.Parameters.Add("@AdImg", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Disabled", SqlDbType.Bit);
        cmd.Parameters.Add("@AdID", SqlDbType.Decimal).Direction = ParameterDirection.Output;

        cmd.Parameters["@AdText"].Value = dto.AdText;
        cmd.Parameters["@AdUrl"].Value = dto.AdUrl;
        cmd.Parameters["@AdImg"].Value = dto.AdImg;
        cmd.Parameters["@Disabled"].Value = dto.Disabled;

        DbProvider.ExecCmd(cmd);
        dto.AdID = Convert.ToDecimal(cmd.Parameters["@AdID"].Value);
    }
}
